import pandas as pd
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud

policy = pd.read_csv("policy/policy.csv")


# 去掉停用词
def remove_stop_words(f):
    stop_words = ['通知', '关于', '的通知', '的', '和', '重庆市', '重点']
    for stop_word in stop_words:
        f = f.replace(stop_word, '')
    return f


# 生成词云
def create_word_cloud(f):
    print('根据词频，开始生成词云!')
    f = remove_stop_words(f)
    cut_text = " ".join(jieba.cut(f, cut_all=False, HMM=True))
    wc = WordCloud(
        font_path="./wc.ttf",
        max_words=100,
        width=2000,
        height=1200,
    )
    print(cut_text)
    word_cloud = wc.generate(cut_text)
    # 写词云图片
    word_cloud.to_file("word_cloud_title.jpg")
    # 显示词云文件
    plt.imshow(word_cloud)
    plt.axis("off")
    plt.show()


all_word = ""
for i in policy.values.tolist():
    all_word = all_word + i[1]
create_word_cloud(all_word)
